﻿using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace ActionLogic
{
    public class TimingExecutableAction : ITimingExecutable
    {
        private static readonly ILog logger = LogManager.GetLogger(typeof(TimingExecutableAction));
        private readonly IExecutable _executable;
        private readonly Timer _timer;

        public long TimeInterval { get; set; }

        public TimingExecutableAction(IExecutable executable, int interval)
        {
            _executable = executable;
            TimeInterval = interval;

            _timer = new Timer {Enabled = false, AutoReset = true};
            _timer.Elapsed += (sender, e) => ActionExecutor.GetInstance().Add(_executable);
        }

        public void Start()
        {
            logger.Info("TimingExecutableAction Starts");
            _timer.Interval = TimeInterval;
            _timer.Enabled = true;
        }

        public void Pause()
        {
            logger.Info("TimingExecutableAction Pause");
            _timer.Enabled = false;
        }

        public void Stop()
        {
            logger.Info("TimingExecutableAction Stop");
            _timer.Enabled = false;
        }
    }
}